﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Windows.Forms;

namespace Donate.DAl
{
    class userNameEntity
    {

        public String ID { get; set; }
        public String user_Name { get; set; }
    }
    class userNames
    {
        private static String conString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='Donates.accdb' ;Jet OLEDB:Database Password=Yarabe11";
       //private static string conString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + (System.Reflection.Assembly.GetExecutingAssembly().Location).Remove((System.Reflection.Assembly.GetExecutingAssembly().Location).IndexOf("Tayseer.exe")) + "Tayseer.accdb;Jet OLEDB:Database Password=Yarabe11";
       private static String cmdText = "";

       public static Boolean insert(userNameEntity m)
       {
           bool flag = true;

           cmdText = "INSERT INTO userName (ID ,user_Name) values (?,?)";

           OleDbConnection con = new OleDbConnection(conString);
           OleDbCommand cmd = new OleDbCommand(cmdText, con);

           cmd.Parameters.AddWithValue("@ID", m.ID);
           cmd.Parameters.AddWithValue("@user_Name", m.user_Name);

           try
           {
               con.Open();
               Int32 x = cmd.ExecuteNonQuery();
               if (x == 0)
               {
                   flag = false;
               }
           }
           catch (Exception ex)
           {
               MessageBox.Show("error" + ex.Message);
               flag=false;
           }
           finally
           {
               if (con.State == ConnectionState.Open)
               {
                   con.Close();
               }

           }
           return flag;

       }
       public static Boolean update(userNameEntity m)
       {
           bool flag = true;

           cmdText = "Update userName set ID=? ,user_Name= ? where ID =?";

           OleDbConnection con = new OleDbConnection(conString);
           OleDbCommand cmd = new OleDbCommand(cmdText, con);

           cmd.Parameters.AddWithValue("@ID", m.ID);
           cmd.Parameters.AddWithValue("@user_Name", m.user_Name);

           try
           {
               con.Open();
               Int32 x = cmd.ExecuteNonQuery();
               if (x == 0)
               {
                   flag = false;
               }
           }
           catch (Exception ex)
           {
               MessageBox.Show("error in update" + ex.Message);
                 flag=false;
           }
           finally
           {
               if (con.State == ConnectionState.Open)
               {
                   con.Close();
               }

           }
           return flag;

       }
       public static Boolean Delete(string user_Name)
       {
           bool flag = true;

           cmdText = "delete from userName where user_Name= ?  ";

           OleDbConnection con = new OleDbConnection(conString);
           OleDbCommand cmd = new OleDbCommand(cmdText, con);

           cmd.Parameters.AddWithValue("@user_Name", user_Name);

           try
           {
               con.Open();
               Int32 x = cmd.ExecuteNonQuery();
               if (x == 0)
               {
                   flag = false;
               }
           }
           catch (Exception ex)
           {
               MessageBox.Show("error in del" + ex.Message);
               flag=false;
           }
           finally
           {
               if (con.State == ConnectionState.Open)
               {
                   con.Close();
               }

           }
           return flag;

       }
       public static userNameEntity getById(Int32 id)
       {
           userNameEntity m = null;
           cmdText = "Select * from userName where ID = ?";

           OleDbConnection con = new OleDbConnection(conString);
           OleDbCommand cmd = new OleDbCommand(cmdText, con);

           cmd.Parameters.AddWithValue("@ID", id);

           try 
           {
               con.Open();
               OleDbDataReader reader = cmd.ExecuteReader();
               m = FillEntityFromReader(reader);
               reader.Close();
           }
           catch (Exception ex)
           {
               MessageBox.Show("userNameEntity error" + ex.Message);
           }
           finally
           {
                     if (con.State == ConnectionState.Open)
                    con.Close();
           }
           return m;
       }
       public static List <userNameEntity> getAll( )
       {
           userNameEntity c;
           List<userNameEntity> m = new List<userNameEntity>();
           cmdText = "Select * from userName ";

           OleDbConnection con = new OleDbConnection(conString);
           OleDbCommand cmd = new OleDbCommand(cmdText, con);



           try
           {
               con.Open();
               OleDbDataReader reader = cmd.ExecuteReader();
               while (reader.Read())
               {
                   c = FillEntityFromReader(reader);
                   m.Add(c);
               }
               reader.Close();
           }
           catch (Exception ex)
           {
               MessageBox.Show("Get all userNameEntity error" + ex.Message);
           }
           finally
           {
               if (con.State == ConnectionState.Open)
                   con.Close();
           }
           return m;
       }
       public static List<userNameEntity> GetByMemName(String mem_Name)
       {
           List<userNameEntity> listEntity = new List<userNameEntity>();
           userNameEntity t = null;
           cmdText = "Select * from userName where user_Name =?  ";

           OleDbConnection con = new OleDbConnection(conString);
           OleDbCommand cmd = new OleDbCommand(cmdText, con);
           cmd.Parameters.AddWithValue("@userName", mem_Name);
          

           try
           {
               con.Open();
               OleDbDataReader reader = cmd.ExecuteReader();
               while (reader.Read())
               {
                   t = FillEntityFromReader(reader);
                   listEntity.Add(t);

               }
               reader.Close();
           }
           catch (Exception ex)
           {
               MessageBox.Show("GetByMemName ", ex.Message);
           }
           finally
           {
               if (con.State == ConnectionState.Open)
                   con.Close();
           }

           return listEntity;




       }
       public static List<userNameEntity> GetByMemNameAndId(String mem_Name,String id)
       {
           List<userNameEntity> listEntity = new List<userNameEntity>();
           userNameEntity t = null;
           cmdText = "Select * from userName where user_Name =? and ID =? ";

           OleDbConnection con = new OleDbConnection(conString);
           OleDbCommand cmd = new OleDbCommand(cmdText, con);
           cmd.Parameters.AddWithValue("@userName", mem_Name);
           cmd.Parameters.AddWithValue("@ID", id);

           try
           {
               con.Open();
               OleDbDataReader reader = cmd.ExecuteReader();
               while (reader.Read())
               {
                   t = FillEntityFromReader(reader);
                   listEntity.Add(t);

               }
               reader.Close();
           }
           catch (Exception ex)
           {
               MessageBox.Show("GetByMemName ", ex.Message);
           }
           finally
           {
               if (con.State == ConnectionState.Open)
                   con.Close();
           }

           return listEntity;




       }
       public static userNameEntity FillEntityFromReader(OleDbDataReader reader)
       {

           userNameEntity m = new userNameEntity();
           if (reader["ID"] != null)
               m.ID = reader["ID"].ToString();
           if (m.user_Name != null)
               m.user_Name = reader["user_Name"].ToString();
           return m;
       }
   }
    }

